﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;

namespace Perceiveit.Xml
{
    public abstract class XRootEntity : XEntity
    {
        private XName _rootName;

        public XName RootName
        {
            get { return _rootName; }
        }


        /// <summary>
        /// Gets the XDocument associated with this root entity
        /// </summary>
        public XDocument Document
        {
            get { return this.Element.Document; }
        }

        public XRootEntity(XName rootName)
        {
            _rootName = rootName;
        }


        /// <summary>
        /// Initializes the XRootEntity with the specified
        /// XDocument. If there is already an XDocument associated with 
        /// this root entity it will be replaced
        /// </summary>
        /// <param name="doc">The XDocument to initialize this root entity with (cannot be null)</param>
        public void Init(XDocument doc)
        {
            if (null == doc)
                throw new ArgumentNullException("doc");
            if (null == doc.Root)
                throw new ArgumentNullException("document.Root");
            this.Init(doc.Root);
        }

        protected override XElement CreateEmptyXElement()
        {
            XDocument doc = new XDocument();
            XElement ele = new XElement(this.RootName);
            doc.Add(ele);
            return ele;
        }

        
    }
}
